package com.sky.controller;

import com.sky.domain.Admin;
import com.sky.domain.LoginForm;
import com.sky.domain.Student;
import com.sky.domain.Teacher;
import com.sky.service.AdminService;
import com.sky.service.StudentService;
import com.sky.service.TeacherService;
import com.sky.util.CreateVerifiCodeImage;
import org.apache.ibatis.annotations.ResultMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 尹稳健~
 * @description: 控制器-管理系统登录与主页面
 * @version 1.0
 */

@Controller
@RequestMapping("/system")
public class SystemController {

    //注入业务对象
    @Autowired
    @Qualifier("adminServiceImpl")
    private AdminService adminService;

    //注入业务对象
    @Autowired
    @Qualifier("studentServiceImpl")
    private StudentService studentService;

    //注入业务对象
    @Autowired
    @Qualifier("teacherServiceImpl")
    private TeacherService teacherService;

    //存储返回给页面的对象数据
    private Map<String,Object> result = new HashMap<String, Object>();

    /**
     * @description: 跳转到用户登录页面
     * @param: no
     * @return: java.lang.String
     */
    @RequestMapping("/goLogin")
    public String goLogin(){
        return "system/login";
    }

    /**
     * @description: 获取并显示验证码图片
     * @param: request
     * @param: response
     * @return: void
     */
    @RequestMapping("/getVerifiCodeImage")
    public void getVerifiCodeImage(HttpServletRequest request, HttpServletResponse response){
        // 验证码图片
        BufferedImage verifiCodeImage = CreateVerifiCodeImage.getVerifiCodeImage();
        // 验证码字符串
        String verifiCode = String.valueOf(CreateVerifiCodeImage.getVerifiCode());
        try {
            // 将图片写入网页
            ImageIO.write(verifiCodeImage,"JPEG",response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 将验证码字符串写入session域保存
        request.getSession().setAttribute("verifiCode",verifiCode);
    }

    /**
     * @description: 验证用户登录信息
     * @param: loginForm
     * @param: request
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     */
    @RequestMapping("/login")
    @ResponseBody
    public Map<String,Object> login(LoginForm loginForm,HttpServletRequest request){
        // 接受session中的验证码字符串
        String verifiCode = (String)request.getSession().getAttribute("verifiCode");
        System.out.println(verifiCode);
        // 验证前端传入的字符串和后台保存的验证码字符串进行比对
        if ("".equals(loginForm.getVerifiCode())){
            result.put("success",false);
            result.put("msg","长时间为操作,会话已失效,请刷新页面后重试!");
            return result;
        }else if (!loginForm.getVerifiCode().equalsIgnoreCase(verifiCode)){
            result.put("success",false);
            result.put("msg","验证码有误!请重试!");
            return result;
        }
        System.out.println("验证码正确");
        request.getSession().removeAttribute("verifiCode");
        //效验用户登录信息
        switch (loginForm.getUserType()){
            //管理员身份
            case 1:
                try {
                    Admin admin = adminService.login(loginForm);
//                    System.out.println(admin);
                    if (admin!=null){
                        HttpSession session = request.getSession();
                        session.setAttribute("userInfo",admin);
                        session.setAttribute("userType",loginForm.getUserType());
                        result.put("success",true);
                        return result;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    result.put("success", false);
                    result.put("msg", "登录失败! 服务器端发生异常!");
                    return result;
                }
                break;
            //学生身份
            case 2:
                try {
                    Student student = studentService.login(loginForm);
//                    System.out.println(admin);
                    if (student!=null){
                        // 这里传参session为了不同用户看到不同页面
                        HttpSession session = request.getSession();
                        session.setAttribute("userInfo",student);
                        session.setAttribute("userType",loginForm.getUserType());
                        result.put("success",true);
                        return result;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    result.put("success", false);
                    result.put("msg", "登录失败! 服务器端发生异常!");
                    return result;
                }
                break;
            //教师身份
            case 3:
                try {
                    Teacher teacher = teacherService.login(loginForm);
//                    System.out.println(admin);
                    if (teacher!=null){
                        HttpSession session = request.getSession();
                        session.setAttribute("userInfo",teacher);
                        session.setAttribute("userType",loginForm.getUserType());
                        result.put("success",true);
                        return result;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    result.put("success", false);
                    result.put("msg", "登录失败! 服务器端发生异常!");
                    return result;
                }
                break;
        }
        //登录失败
        result.put("success",false);
        result.put("msg", "用户名或密码错误!");
        return result;
    }

    /**
     * @description: 跳转到系统主页面
     * @param: no
     * @return: java.lang.String
     */
    @RequestMapping("/goSystemMainView")
    public String goSystemMainView(){
        return "/system/main";
    }

    /**
     * @description: 注销用户信息
     * @param: request
     * @return: void
     */
    @RequestMapping("/loginOut")
    public String loginOut(){
        // 重定向到登录界面
        return "redirect:/system/goLogin";
    }
}
